package com.baizhi.security;

import com.baizhi.service.BzAdminService;
import com.baizhi.service.impl.BzAdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author yu hao
 * @since 2020/10/14 8:47
 */
/*开启SpringSecurity注解式开发*/
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private BzAdminServiceImpl bzAdminServiceImpl;
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(bzAdminServiceImpl)
        .passwordEncoder(bCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/jsp/login.jsp","/login","/getValidate","/css/**","/img/**","/js/**","/lay/**","/layui/**","/ztree/**")
                .permitAll()
                /*改为注解式开发*/
                /*.antMatchers("/bzMenu")
                .hasAnyRole("vip")*/
                .anyRequest()
                .authenticated();

        http.formLogin()
                .successForwardUrl("/toMain")
                .failureForwardUrl("/jsp/login.jsp")
                .loginPage("/jsp/login.jsp")
                .loginProcessingUrl("/login")
                .and()
                .csrf()
                .disable();
        http.headers().frameOptions().disable();
    }
}
